//
//  MockPedometer.swift
//  FitNessTests
//
//  Created by lanjing on 2021/4/7.
//  Copyright © 2021 Razeware. All rights reserved.
//

import CoreMotion
@testable import FitNess
class MockPedometer: Pedometer{
  static let notAuthorizedError = NSError(domain: CMErrorDomain, code: Int(CMErrorMotionActivityNotAuthorized.rawValue), userInfo: nil)
  
  private(set) var started: Bool = false
  
  var pedometerAvailable: Bool = true
  var permissionDeclined = false
  var error: Error?
  
  var updateBlock: ((Error?) -> Void)?
  var dataBlock:((PedometerData?, Error?) -> Void)?
  
//  func start(completion: @escaping (Error?) -> Void) {
//    started = true
//
//    //模拟异步错误的发生
//    DispatchQueue.global(qos: .default).async {
//      completion(self.error)
//    }
//  }
  
  func start(dataUpdates: @escaping (PedometerData?, Error?) -> Void,
             eventUpdates: @escaping (Error?) -> Void){
    started = true
    updateBlock = eventUpdates
    dataBlock = dataUpdates
    
    DispatchQueue.global(qos: .default).async {
      self.updateBlock?(self.error)
    }
  }
  
  func sendData(_ data: PedometerData){
    dataBlock?(data, error)
  }
  
}

struct MockPedometerData: PedometerData{
  let steps: Int
  let distanceTravelled: Double
}
